package com.platform.sys.core.util;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.log.Log;
import com.platform.core.exception.ServiceException;
import com.platform.sys.modular.consts.enums.SysConfigExceptionEnum;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * 设置生成代码所需要数据的上下文
 *
 */
public class MysqlUtil {

    private static final Log log = Log.get();

    private static final String DATA_SOURCE_SQL = " select table_name,create_time,update_time,table_comment from information_schema.tables where table_schema = ?";

    private static final String URL = "url";

    private static final String ACCOUNT = "account";

    private static final String PWASSWORD = "password";


    public static void onApplicationEvent() {

        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取数据库连接配置
            String dataSourceUrlCurrent = "jdbc:mysql://10.0.4.29:3307/sk_platform_account_db?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true";
            String dataSourceUsernameCurrent = "dev";
            String dataSourcePasswordCurrent = "@Changjiu99";

            conn = DriverManager.getConnection(dataSourceUrlCurrent, dataSourceUsernameCurrent, dataSourcePasswordCurrent);

            List<Entity> dataSourceEntity = SqlExecutor.query(conn, DATA_SOURCE_SQL, new EntityListHandler(),"sk_platform_account_db");

            for(Entity entity : dataSourceEntity){
                System.out.println(entity.get("table_name"));
            }

        } catch (SQLException | ClassNotFoundException e) {
            log.error(">>> 读取数据库constants配置信息出错：");
            e.printStackTrace();
            throw new ServiceException(SysConfigExceptionEnum.DATA_SOURCE_NOT_EXIST);
        } finally {
            DbUtil.close(conn);
        }

    }
}
